//
//  LeftScrollView.m
//  dyseller
//
//  Created by 红喇叭 on 2017/11/18.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "LeftScrollView.h"

@interface LeftScrollView()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *_labelTabelView;
    NSInteger selectRow;
    UIView *_lineView;
}

@end

@implementation LeftScrollView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = ControllerColor;
        selectRow = 0;
        [self labelTabelView];
        if(_allLabelData.count<1) _allLabelData = @[@"菜品"].mutableCopy;
        _labelTabelView.tableFooterView = [UIView new];
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 2, 35)];
        _lineView.backgroundColor = MianRedColor;
        [_labelTabelView addSubview:_lineView];
     }
    return self;
}

- (void)setAllLabelData:(NSArray *)allLabelData{
    
    _allLabelData = allLabelData;
    [_labelTabelView reloadData];
    
    NSIndexPath * selIndex = [NSIndexPath indexPathForRow:selectRow inSection:0];
    [_labelTabelView selectRowAtIndexPath:selIndex animated:YES scrollPosition:UITableViewScrollPositionNone];
  //  [self tableView:_labelTabelView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:selectRow inSection:0]];
    _lineView.y = 10 + 55*selectRow;
    

}

- (void)selectRow:(NSInteger)row{
     [_labelTabelView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
}

- (UITableView *)labelTabelView{
    if(_labelTabelView==nil){
        _labelTabelView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _labelTabelView.dataSource = self;
        _labelTabelView.delegate = self;
        _labelTabelView.estimatedRowHeight = 60;
        _labelTabelView.estimatedSectionFooterHeight = 0;
        _labelTabelView.estimatedSectionHeaderHeight = 0;
        _labelTabelView.backgroundColor = ControllerColor;
        _labelTabelView.showsVerticalScrollIndicator = NO;//滑动时不显示滑动条
        [_labelTabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"leftScrollCell"];
        [self addSubview:_labelTabelView];
    }
    return _labelTabelView;
}


#pragma  mark ========UITabelViewdelegate UITableDataSource========

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _allLabelData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"leftScrollCell"];

    UILabel *label = [[UILabel alloc] init];
    label.text = _allLabelData[indexPath.row];
    label.textColor = UIColorFromRGB(0x333333);
    label.font = font13;
    
    //label.textColor = MianRedColor;
    [cell.contentView addSubview:label];
    cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
    label.sd_layout.leftSpaceToView(cell.contentView, GETWITH(20)).centerYEqualToView(cell.contentView).rightSpaceToView(cell.contentView, GETWITH(0)).autoHeightRatio(0);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  //  [tableView deselectRowAtIndexPath:indexPath animated:YES];
    selectRow = indexPath.row;
    [UIView animateWithDuration:0.25 animations:^{
        _lineView.y = 10 + 55*indexPath.row;
    } completion:^(BOOL finished) {
        if([_delegate respondsToSelector:@selector(leftScrollSelectIndex:)]){
            [_delegate leftScrollSelectIndex:indexPath];
        }

    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

@end
